$(function(){
	var data = $("#tabContent > .tab-pane.active").data();
	//获取当前选中tab页的tabId
	$("#navTabs").find("li").each(function(i){
		$(this).on("click", function () {
			scrollTo(0,0);
			tabId=$("#tabContent > .tab-pane.active").attr("id");
         });
	});
	var depCode = data.depCode;
	var param = data.param;
	var AvgDaysL=0;
	var AvgDaysH=0;
	var AvgFeeL=0;
	var AvgFeeH=0;
	var HCAvgFeeL=0;
	var HCAvgFeeH=0;
	var DrugFeeL=0;
	var DrugFeeH=0;
	var HCDrugFeeL=0;
	var HCDrugFeeH=0;
	var array = param.split("-");
	for(var i=0; i<array.length; i++){
		var as = array[i].split(":");
		if(as[0]=="AvgDays"){
			AvgDaysL=as[1];
			AvgDaysH=as[2];
		}
		if(as[0]=="AvgFee"){
			AvgFeeL=as[1];
			AvgFeeH=as[2];
		}
		if(as[0]=="HCAvgFee"){
			HCAvgFeeL=as[1];
			HCAvgFeeH=as[2];
		}
		if(as[0]=="DrugFee"){
			DrugFeeL=as[1];
			DrugFeeH=as[2];
		}
		if(as[0]=="HCDrugFee"){
			HCDrugFeeL=as[1];
			HCDrugFeeH=as[2];
		}
	}
	
	$("#"+tabId+" #mergeKeyDisRatio").click(function(){
		var keyDisRatio = $("#"+tabId+" #keyDisRatioId").val();
		if(!keyDisRatio){
			keyDisRatio=80;
		}
		$.post(contextPath+"/ForeCast/loadDepDisParam.dhc", {pardepCode:depCode, type1: keyDisRatio, mrLot: mrLotForeCast})
		.done(function(result){
			var columns=[ 
	                     { "data": 'drgCode1', "width": "16%",
		                       "render": function(obj, type, full){
		                    	   var drgDesc = full.drgDesc;
		                    	   drgDesc = drgDesc.substring(0, 5) + "..."
		                    	   return "<input type='checkbox' />  <span title='"+full.drgDesc+"'>"+full.drgCode+"("+drgDesc+")</span>"
		                       }
		                     },
		                     { "data": 'TotalCaseCnt', "width": "4%"},
	 	                     { "data": "AvgDaysValue", "width": "4%"},
	 	                     { "data": "AvgDaysBenchmark", "width": "4%"},
	 	                     { "data": "AvgDaysParam" , "width": "4%",
   	                       "render": function(obj, type, full) {
   	                    	   var value1 = full.AvgDaysValue?full.AvgDaysValue:0;
   	                    	   var value2 = full.AvgDaysBenchmark?full.AvgDaysBenchmark:0;
   	                    	   var value = full.AvgDaysL?full.AvgDaysL:AvgDaysH;
   	                    	   if(parseFloat(value1)<=parseFloat(value2)){
   	                    		  value = full.AvgDaysL?full.AvgDaysL:AvgDaysL;
   	                    	   }
   	                    	   return "<input id='"+full.drgCode+"_AvgDaysL' type='text' style='width:50px' value='"+value+"'></input>";
	  	                         }
	   	                     },
	  	                     { "data": "AvgDaysAdjust" , "width": "4%",
	  	                       "render": function(obj, type, full) {
	  	                    	   		var value = full.AvgDaysH?full.AvgDaysH:0;
	  	                    	   		return "<input id='"+full.drgCode+"_AvgDaysH' type='text' style='width:50px' value='"+value+"'></input>";
	  	                         	}
		                     },
		                     { "data": "AvgFeeValue", "width": "4%"},
	 	                     { "data": "AvgFeeBenchmark", "width": "4%"},
	 	                     { "data": "AvgFeeParam" , "width": "4%",
   	                       "render": function(obj, type, full) {
   	                    	   var value1 = full.AvgFeeValue?full.AvgFeeValue:0;
   	                    	   var value2 = full.AvgFeeBenchmark?full.AvgFeeBenchmark:0;
   	                    	   var value = full.AvgFeeL?full.AvgFeeL:AvgFeeH;
   	                    	   if(parseFloat(value1)<=parseFloat(value2)){
   	                    		  value = full.AvgFeeL?full.AvgFeeL:AvgFeeL;
   	                    	   }
   	                    	   return "<input id='"+full.drgCode+"_AvgFeeL' type='text' style='width:50px' value='"+value+"'></input>";
	  	                         }
	   	                     },
	  	                     { "data": "AvgFeeAdjust" , "width": "4%",
	  	                       "render": function(obj, type, full) {
	  	                    	   		var value = full.AvgFeeH?full.AvgFeeH:0;
	  	                    	   		return "<input id='"+full.drgCode+"_AvgFeeH' type='text' style='width:70px' value='"+value+"'></input>";
	  	                         	}
		                     },
		                     { "data": "HCAvgFeeValue", "width": "4%"},
	 	                     { "data": "HCAvgFeeBenchmark", "width": "4%"},
	 	                     { "data": "HCAvgFeeParam" , "width": "4%",
   	                       "render": function(obj, type, full) {
   	                    	   var value1 = full.HCAvgFeeValue?full.HCAvgFeeValue:0;
   	                    	   var value2 = full.HCAvgFeeBenchmark?full.HCAvgFeeBenchmark:0;
   	                    	   var value = full.HCAvgFeeL?full.HCAvgFeeL:HCAvgFeeH;
   	                    	   if(parseFloat(value1)<=parseFloat(value2)){
   	                    		  value = full.HCAvgFeeL?full.HCAvgFeeL:HCAvgFeeL;
   	                    	   }
   	                    	   return "<input id='"+full.drgCode+"_HCAvgFeeL' type='text' style='width:50px' value='"+value+"'></input>";
	  	                         }
	   	                     },
	  	                     { "data": "HCAvgFeeAdjust" , "width": "4%",
	  	                       "render": function(obj, type, full) {
	  	                    	   		var value = full.HCAvgFeeH?full.HCAvgFeeH:0;
	  	                    	   		return "<input id='"+full.drgCode+"_HCAvgFeeH' type='text' style='width:70px' value='"+value+"'></input>";
	  	                         	}
		                     },
		                     { "data": "DrugFeeValue", "width": "4%"},
	 	                     { "data": "DrugFeeBenchmark", "width": "4%"},
	 	                     { "data": "DrugFeeParam" , "width": "4%",
   	                       "render": function(obj, type, full) {
   	                    	   var value1 = full.DrugFeeValue?full.DrugFeeValue:0;
   	                    	   var value2 = full.DrugFeeBenchmark?full.DrugFeeBenchmark:0;
   	                    	   var value = full.DrugFeeL?full.DrugFeeL:DrugFeeH;
   	                    	   if(parseFloat(value1)<=parseFloat(value2)){
   	                    		  value = full.DrugFeeL?full.DrugFeeL:DrugFeeL;
   	                    	   }
   	                    	   return "<input id='"+full.drgCode+"_DrugFeeL' type='text' style='width:50px' value='"+value+"'></input>";
	  	                         }
	   	                     },
	  	                     { "data": "DrugFeeAdjust" , "width": "4%",
	  	                       "render": function(obj, type, full) {
	  	                    	   		var value = full.DrugFeeH?full.DrugFeeH:0;
	  	                    	   		return "<input id='"+full.drgCode+"_DrugFeeH' type='text' style='width:70px' value='"+value+"'></input>";
	  	                         	}
		                     },
		                     { "data": "HCDrugFeeValue", "width": "4%"},
	 	                     { "data": "HCDrugFeeBenchmark", "width": "4%"},
	 	                     { "data": "HCDrugFeeParam" , "width": "4%",
   	                       "render": function(obj, type, full) {
   	                    	   var value1 = full.HCDrugFeeValue?full.HCDrugFeeValue:0;
   	                    	   var value2 = full.HCDrugFeeBenchmark?full.HCDrugFeeBenchmark:0;
   	                    	   var value = full.HCDrugFeeL?full.HCDrugFeeL:HCDrugFeeH;
   	                    	   if(parseFloat(value1)<=parseFloat(value2)){
   	                    		  value = full.HCDrugFeeL?full.HCDrugFeeL:HCDrugFeeL;
   	                    	   }
   	                    	   return "<input id='"+full.drgCode+"_HCDrugFeeL' type='text' style='width:50px' value='"+value+"'></input>";
	  	                         }
	   	                     },
	  	                     { "data": "HCDrugFeeAdjust" , "width": "4%",
	  	                       "render": function(obj, type, full) {
	  	                    	   		var value = full.HCDrugFeeH?full.HCDrugFeeH:0;
	  	                    	   		return "<input id='"+full.drgCode+"_HCDrugFeeH' type='text' style='width:70px' value='"+value+"'></input>";
	  	                         	}
		                     }
		                 ];
			var paramsDataTables={lengthChange:false,info:false,ordering:false,rownumbers:true,columnDefs:[{"orderable": false,"targets": [ 0 ] },{ "visible": false, "targets": [0]}]};
			$("#"+tabId+" #paramDepDis-table").dataTableBasic(columns,data,paramsDataTables);
			
			$("#"+tabId+" #paramDepDis-table tbody").find("tr").each(function(i, n){
				//平均住院日参数计算
				$(n).find("td").eq(4).find("input").change(function(){
					var value = $(n).find("td").eq(2).html();
					var valueb = $(n).find("td").eq(3).html();
					var param = $(n).find("td").eq(4).find("input").val();
					var valuea = 0;
					if(param=="0" || valueb==""){
						$(n).find("td").eq(5).find("input").val(value);
					} else {
						if(value!=""){
							var valueF = parseFloat(value);
							var valuebF = parseFloat(valueb);
							valuea = valueF-Math.abs(valueF-valuebF)*(parseFloat(param)/100)
						}
						$(n).find("td").eq(5).find("input").val(valuea.toFixed(2));
					}
				});
				//次均费用参数计算
				$(n).find("td").eq(8).find("input").change(function(){
					var value = $(n).find("td").eq(6).html();
					var valueb = $(n).find("td").eq(7).html();
					var param = $(n).find("td").eq(8).find("input").val();
					var valuea = 0;
					if(param=="0" || valueb==""){
						$(n).find("td").eq(9).find("input").val(value);
					} else {
						if(value!=""){
							var valueF = parseFloat(value);
							var valuebF = parseFloat(valueb);
							valuea = valueF-Math.abs(valueF-valuebF)*(parseFloat(param)/100)
						}
						$(n).find("td").eq(9).find("input").val(valuea.toFixed(2));
					}
				});
				//医保次均费用计算
				$(n).find("td").eq(12).find("input").change(function(){
					var value = $(n).find("td").eq(10).html();
					var valueb = $(n).find("td").eq(11).html();
					var param = $(n).find("td").eq(12).find("input").val();
					var valuea = 0;
					if(param=="0" || valueb==""){
						$(n).find("td").eq(13).find("input").val(value);
					} else {
						if(value!=""){
							var valueF = parseFloat(value);
							var valuebF = parseFloat(valueb);
							valuea = valueF-Math.abs(valueF-valuebF)*(parseFloat(param)/100)
						}
						$(n).find("td").eq(13).find("input").val(valuea.toFixed(2));
					}
				});
				//药品费用计算
				$(n).find("td").eq(16).find("input").change(function(){
					var value = $(n).find("td").eq(14).html();
					var valueb = $(n).find("td").eq(15).html();
					var param = $(n).find("td").eq(16).find("input").val();
					var valuea = 0;
					if(param=="0" || valueb==""){
						$(n).find("td").eq(17).find("input").val(value);
					} else {
						if(value!=""){
							var valueF = parseFloat(value);
							var valuebF = parseFloat(valueb);
							valuea = valueF-Math.abs(valueF-valuebF)*(parseFloat(param)/100)
						}
						$(n).find("td").eq(17).find("input").val(valuea.toFixed(2));
					}
				});
				
				//次均药品费用计算
				$(n).find("td").eq(20).find("input").change(function(){
					var value = $(n).find("td").eq(18).html();
					var valueb = $(n).find("td").eq(19).html();
					var param = $(n).find("td").eq(20).find("input").val();
					var valuea = 0;
					if(param=="0" || valueb==""){
						$(n).find("td").eq(21).find("input").val(value);
					} else {
						if(value!=""){
							var valueF = parseFloat(value);
							var valuebF = parseFloat(valueb);
							valuea = valueF-Math.abs(valueF-valuebF)*(parseFloat(param)/100)
						}
						$(n).find("td").eq(21).find("input").val(valuea.toFixed(2));
					}
				});
			});
			
			$("#"+tabId+" #paramDepDis-table tbody").find("tr").each(function(i, n){
				$(n).find("td").eq(4).find("input").change();
				$(n).find("td").eq(8).find("input").change();
				$(n).find("td").eq(12).find("input").change();
				$(n).find("td").eq(16).find("input").change();
				$(n).find("td").eq(20).find("input").change();
			});
			
			$("#"+tabId+" #mergeParamOk").click(function(){
				$("#"+tabId+" #paramDepDis-table tbody").find("tr").each(function(i, n){
					if($("#"+tabId+" #paramQuotaCheck").val()=="AvgDays"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							var value = $(n).find("td").eq(2).html();
							var valueq = $(n).find("td").eq(3).html();
							if(value!="" && valueq!=""){
								if(parseFloat(value)>parseFloat(valueq)){
									$(n).find("td").eq(4).find("input").val($("#"+tabId+" #overManageID").val());
								}else{
									$(n).find("td").eq(4).find("input").val($("#"+tabId+" #notOverManageID").val());
								}
							}
						}
					}
					if($("#"+tabId+" #paramQuotaCheck").val()=="AvgFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							var value = $(n).find("td").eq(6).html();
							var valueq = $(n).find("td").eq(7).html();
							if(value!="" && valueq!=""){
								if(parseFloat(value)>parseFloat(valueq)){
									$(n).find("td").eq(8).find("input").val($("#"+tabId+" #overManageID").val());
								}else{
									$(n).find("td").eq(8).find("input").val($("#"+tabId+" #notOverManageID").val());
								}
							}
						}
					}
					if($("#"+tabId+" #paramQuotaCheck").val()=="HCAvgFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							var value = $(n).find("td").eq(10).html();
							var valueq = $(n).find("td").eq(11).html();
							if(value!="" && valueq!=""){
								if(parseFloat(value)>parseFloat(valueq)){
									$(n).find("td").eq(12).find("input").val($("#"+tabId+" #overManageID").val());
								}else{
									$(n).find("td").eq(12).find("input").val($("#"+tabId+" #notOverManageID").val());
								}
							}
						}
					}
					if($("#"+tabId+" #paramQuotaCheck").val()=="DrugFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							var value = $(n).find("td").eq(14).html();
							var valueq = $(n).find("td").eq(15).html();
							if(value!="" && valueq!=""){
								if(parseFloat(value)>parseFloat(valueq)){
									$(n).find("td").eq(16).find("input").val($("#"+tabId+" #overManageID").val());
								}else{
									$(n).find("td").eq(16).find("input").val($("#"+tabId+" #notOverManageID").val());
								}
							}
						}
					}
					if($("#"+tabId+" #paramQuotaCheck").val()=="HCDrugFee"){
						if($("#"+tabId+" input[name='checkAll']:checked").val()==1 || ($("#"+tabId+" input[name='checkAll']:checked").val()==0 && $(n).find("td").eq(0).find("input[type='checkbox']").is(':checked'))){
							var value = $(n).find("td").eq(18).html();
							var valueq = $(n).find("td").eq(19).html();
							if(value!="" && valueq!=""){
								if(parseFloat(value)>parseFloat(valueq)){
									$(n).find("td").eq(20).find("input").val($("#"+tabId+" #overManageID").val());
								}else{
									$(n).find("td").eq(20).find("input").val($("#"+tabId+" #notOverManageID").val());
								}
							}
						}
					}
				})
				
				$("#"+tabId+" #paramDepDis-table tbody").find("tr").each(function(i, n){
					$(n).find("td").eq(4).find("input").change();
					$(n).find("td").eq(8).find("input").change();
					$(n).find("td").eq(12).find("input").change();
					$(n).find("td").eq(16).find("input").change();
					$(n).find("td").eq(20).find("input").change();
				});
			})
		});
	});
	
	$("#"+tabId+" #mergeKeyDisRatio").click();
	
	$("#"+tabId+" #depDisParamSave_btn").click(function(){
		var str = "";
		$("#"+tabId+" #paramDepDis-table tbody").find("tr").each(function(i, n){
			var drgCode=$(n).find("td").eq(4).find("input").attr("id")?$(n).find("td").eq(4).find("input").attr("id").split("_")[0]:"";
			str += drgCode+"|";
			var avgDaysL=$(n).find("td").eq(4).find("input").val();
			var avgDaysH=$(n).find("td").eq(5).find("input").val();
			str += "AvgDays:"+avgDaysL+":"+avgDaysH;
			str += "-";
			var avgFeeL=$(n).find("td").eq(8).find("input").val();
			var avgFeeH=$(n).find("td").eq(9).find("input").val();
			str += "AvgFee:"+avgFeeL+":"+avgFeeH;
			str += "-";
			var hcavgFeeL=$(n).find("td").eq(12).find("input").val();
			var hcavgFeeH=$(n).find("td").eq(13).find("input").val();
			str += "HCAvgFee:"+hcavgFeeL+":"+hcavgFeeH;
			str += "-";
			var drugFeeL=$(n).find("td").eq(16).find("input").val();
			var drugFeeH=$(n).find("td").eq(17).find("input").val();
			str += "DrugFee:"+drugFeeL+":"+drugFeeH;
			str += "-";
			var hcdrugFeeL=$(n).find("td").eq(20).find("input").val();
			var hcdrugFeeH=$(n).find("td").eq(21).find("input").val();
			str += "HCDrugFee:"+hcdrugFeeL+":"+hcdrugFeeH;
			str += ",";
		})
		str = str.substr(0, str.length-1);
		$.post(contextPath+"/ForeCast/saveParamDepDisData.dhc", {"pardepCode": depCode, "ccode": str})
		.done(function(result){
			if(result.success){
				drgUI.success("保存完毕") ;
			}
		});
	});
})